package com.shensi.adminCollect.service.station.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidParameterSpecException;

/***
 * <Title>  AesDecryptUtil  </Title>
 * <Description> Aes解码工具类 </Description>
 * @author JK
 * @date 2019-11-12
 */
public class AesDecryptUtil {

    private static Logger logger =(Logger) LoggerFactory.getLogger(AesDecryptUtil.class);
    //BouncyCastle: 开源的加解密解决方案 网址:http://www.bouncycastle.org/
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /*
     *  使用AES解码 encryptedData 数据
     * @param encryptedData 解码数据
     * @param session_key   密钥
     * @param iv    偏移量
     * @param encodingFormat 解码后编码方式 像UTF-8等
     * @return 返回String类型字符串: new String(resultBytes, encodingFormat)
     */
    public static JSONObject decryptEncryptedData(String encryptedData, String session_key, String iv, String encodingFormat){

        //被加密的数据
        byte[] edBytes = Base64.decodeBase64(encryptedData.getBytes());
        //加密的秘钥
        byte[] keyBytes = Base64.decodeBase64(session_key.getBytes());
        //偏移量
        byte[] ivBytes = Base64.decodeBase64(iv.getBytes());
        //解码后结果值
        JSONObject resultStr =null;
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
            //java.security 管理特定算法的
            AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("AES");
            algorithmParameters.init(new IvParameterSpec(ivBytes));
            //开始初始化解码
            cipher.init(Cipher.DECRYPT_MODE, keySpec, algorithmParameters);
            byte[] resultBytes = cipher.doFinal(edBytes);
            if (resultBytes !=null && resultBytes.length >0){
//                resultStr = JSON.parseObject(str);
                resultStr = JSONObject.parseObject(new String(resultBytes, encodingFormat));
                logger.info("解密结果：" + resultStr.toString());
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidParameterSpecException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return resultStr;
    }
}
